package com.example.yrdev.ordering_system.Model;

import android.content.Context;


import com.example.yrdev.ordering_system.Database.DingHanDB;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Locale;

/**
 * Created by luke on 15/11/2016.
 */

public class Good implements Serializable {//商品
   public static   final String DH_TABLE_NAME = "GoodList";

    public static   final String item_position="item_position";//商品编号
    public static   final String item_pinyinID="item_pinyinID";//商品名称
    public static  final String item_id="item_id";//数量

    public static  final String item_discountable="tem_discountable";

    public static  final String item_unit="item_unit";
    public static  final String item_storedNumber="item_storedNumber";
    public static  final String item_priceIn="item_priceIn";//0 现金,1 信用卡,2 签单,3 会员卡,4 礼劵, 8 - 退货  会员卡,9 - 退货  现金
    public static  final String item_name="item_name";
    public static  final String item_barcode="item_barcode";//进货价
    public  static final String item_priceout="item_priceout";//会员价
    public static  final String item_warning="item_warning";
    public static  final String item_kind="item_kind";


     public  static final String DH_CREATE_TABLE="CREATE TABLE " + DH_TABLE_NAME + "( " + item_id + " TEXT," + item_name + " TEXT,"
             + item_position + " TEXT," + item_discountable + " TEXT," + item_unit + " TEXT,"    + item_storedNumber + " TEXT, "
             + item_priceIn + " TEXT," + item_pinyinID + " TEXT," + item_barcode + " TEXT,"+ item_priceout + " TEXT,"
             + item_warning + " TEXT," + item_kind + " TEXT" +  ")";

    String position;
    String pinyinID;
    String id;
    String disoountable;
    String unit;
    String storedNumber;
    String priceIn;
    String name;
    String barcode;
    String priceOut;
    String warning;
    String kind;

    Integer purchaseCount=1;//购物数量
    public Good(){

    }
    public Good(String id, String name, String pinyinID){
        this.id=id;
        this.name=name;
        this.pinyinID=pinyinID;

    }

    public Good(JSONObject object){

        try {

            this.id=object.getString("id");
            this.name=object.getString("name");
            this.pinyinID=object.getString("pinYinID");
            this.priceIn=object.getString("priceIn");
            this.priceOut=object.getString("priceOut");
            this.storedNumber=object.getString("storedNumber");
            this.kind=object.getString("kind");
            this.disoountable=object.getString("discountable");


        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }


    }
    public static void storeToDB(Context context, Good good){

        DingHanDB.getInstance(context).insert(DH_TABLE_NAME,good);
    }
    public static void updateGoodStoreNumber(Context context, Good good){
        DingHanDB.getInstance(context).updateGoodStoredNum(good);
    }
    public static ArrayList<Good> getItemDetailList(Context context){
        return DingHanDB.getInstance(context).getGoodDetailList();

    }

    public static Good getCoresspondingGoodDetail(Context context,String goodID){

        return DingHanDB.getInstance(context).getCoresspondingGoodDetail(goodID);

    }



    public static void removeAllGood(Context context){

        DingHanDB.getInstance(context).removeAllGood();

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setStoredNumber(String storedNumber) {
        this.storedNumber = storedNumber;
    }

    public void setPriceIn(String priceIn) {
        this.priceIn = priceIn;
    }

    public void setDisoountable(String disoountable) {
        this.disoountable = disoountable;
    }

    public Integer getPurchaseCount() {
        return purchaseCount;
    }

    public void setPriceOut(String priceOut) {
        this.priceOut = priceOut;
    }

    public String getId() {
        return id;
    }

    public void setPinyinID(String pinyinID) {
        this.pinyinID = pinyinID;
    }

    public void setPurchaseCount(Integer purchaseCount) {
        this.purchaseCount = purchaseCount;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public String getName() {
        return name;
    }

    public String getPinyinID() {
        return pinyinID;
    }

    public String getPriceIn() {
        return priceIn;
    }

    public String getPriceOut() {
        return priceOut;
    }

    public String getStoredNumber() {
        return storedNumber;
    }

    public String getTotalPrice(){
        return  String.format(Locale.getDefault(),"%.2f",purchaseCount* Double.valueOf(priceOut));


    }

    public String getBarcode() {
        return barcode;
    }

    public String getDisoountable() {
        return disoountable;
    }

    public String getUnit() {
        return unit;
    }

    public String getWarning() {
        return warning;
    }

    public String getKind() {
        return kind;
    }

    public String getPosition() {
        return position;
    }
}
